<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>获取跟数据库句柄上一次操作相关的 SQLSTATE</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="pdo.construct.html">« PDO::__construct</a></li>
      <li style="float: right;"><a href="pdo.errorinfo.html">PDO::errorInfo »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.pdo.html">PDO</a></li>
    <li>获取跟数据库句柄上一次操作相关的 SQLSTATE</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="pdo.errorcode" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDO::errorCode</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8, PECL pdo &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">PDO::errorCode</span> &mdash; <span class="dc-title">
   获取跟数据库句柄上一次操作相关的 SQLSTATE
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdo.errorcode-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>PDO::errorCode</strong></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>


 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdo.errorcode-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
  返回一个 SQLSTATE，一个由5个字母或数字组成的在 ANSI SQL 标准中定义的标识符。 简要地说，一个 SQLSTATE 由前面两个字符的类值和后面三个字符的子类值组成。
   class value of 01 indicates a warning and is accompanied by a return code
   of SQL_SUCCESS_WITH_INFO. Class values other than &#039;01&#039;, except for the
   class &#039;IM&#039;, indicate an error.  The class &#039;IM&#039; is specific to warnings
   and errors that derive from the implementation of PDO (or perhaps ODBC,
   if you&#039;re using the ODBC driver) itself.  The subclass value &#039;000&#039; in any
   class indicates that there is no subclass for that SQLSTATE.
  </p>
  <p class="para">
   <span class="function"><strong>PDO::errorCode()</strong></span> only retrieves error codes for operations
   performed directly on the database handle. If you create a PDOStatement
   object through <span class="function"><a href="pdo.prepare.html" class="function">PDO::prepare()</a></span> or
   <span class="function"><a href="pdo.query.html" class="function">PDO::query()</a></span> and invoke an error on the statement
   handle, <span class="function"><strong>PDO::errorCode()</strong></span> will not reflect that error.
   You must call <span class="function"><a href="pdostatement.errorcode.html" class="function">PDOStatement::errorCode()</a></span> to return the error
   code for an operation performed on a particular statement handle.
  </p>
  <p class="para">
   如果数据库句柄没有进行操作，则返回 <strong><code>null</code></strong> 。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdo.errorcode-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1042"><p><strong>示例 #1 取得一个 SQLSTATE 码</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;引发一个错误&nbsp;--&nbsp;BONES&nbsp;数据表不存在&nbsp;*/<br /></span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;bones(skull)&nbsp;VALUES&nbsp;('lucy')"</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #DD0000">"\nPDO::errorCode():&nbsp;"</span><span style="color: #007700">;<br />print&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">errorCode</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
PDO::errorCode(): 42S02
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdo.errorcode-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="pdo.errorinfo.html" class="function" rel="rdfs-seeAlso">PDO::errorInfo()</a> - Fetch extended error information associated with the last operation on the database handle</span></li>
    <li class="member"><span class="function"><a href="pdostatement.errorcode.html" class="function" rel="rdfs-seeAlso">PDOStatement::errorCode()</a> - 获取跟上一次语句句柄操作相关的 SQLSTATE</span></li>
    <li class="member"><span class="function"><a href="pdostatement.errorinfo.html" class="function" rel="rdfs-seeAlso">PDOStatement::errorInfo()</a> - 获取跟上一次语句句柄操作相关的扩展错误信息</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>